package com.hy.main.demo.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Author yang DongYong
 * @Date 2022/4/25 - 04 - 25 - 22:36
 * @Description: com.hy.main.demo.file
 * @version: 1.0
 */
public class FileCopy {


    public static void main(String[] args) throws IOException {
        String path = "E:\\img.jpg";
        String tagGetFile = "D:\\copyImg.jpg";
        System.out.println(copyFiles(path, tagGetFile));
    }

    /**
     * copy文件demo
     *
     * @param path       文件路径
     * @param tarGetPath 要复制的路径
     * @return 是否复制成功
     */
    public static Boolean copyFiles(String path, String tarGetPath) throws IOException {
        Boolean tags = false;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        byte[] buffered = new byte[1024];
        Integer readLength = null;
        try {
            fileInputStream = new FileInputStream(path);
            fileOutputStream = new FileOutputStream(tarGetPath);

            while ((readLength = fileInputStream.read(buffered)) != -1) {
                fileOutputStream.write(buffered,0,readLength);
            }
            return tags = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            fileInputStream.close();
            fileOutputStream.close();
        }
        return tags;
    }
}
